#ifndef H_A2TV0
#define H_A2TV0

#ifndef H_A2GRTXT
# include "a2grtxt.h"
#endif

#ifndef H_A2TVDEF
# include "a2tvdef.h"
#endif

/*----------------- functions and structures --------------*/
/*----------------- variables for  a2tv0kpc ---------------*/
typedef struct
      {char fw;           /* r,l,u,d - tracking direction  */
       int l_b;           /* if cde < l_b  signal          */
       int r_g;           /* registers state               */
       char ew;           /* E - processing end mark       */
       int dsp;           /* TV_PART,TV_FROM,TV_ALL,TV_BLK */
       int cde;           /* error code                    */
       WCB *w;            /* current window                */
       TINT wcor,tk,st,
                 sp,tmp;  /* coordinates of: window, cursor*/
                          /* start, end, current point     */
       int wfr,wto;       /* window constriction for cursor*/
       unsigned int k;    /* code received from keyboard   */
       unsigned int nk;   /* code of automatic exit        */
       unsigned int rk;   /* keyboard registers            */
       unsigned char lt;  /* symbol in ASCII               */
       LDSC *ls, *lc;     /* start of line or substring    */
       unsigned char ch[256];  /* control table            */
       TINT oldsize;           /* old window sizes         */
       int brd_sys;
      } ENV_VRB;
/*--------- bits for r_g field of ENV_VRB structure: ------*/
#define FR_INS      0x01  /* regime of insertion           */
#define FR_LVP      0x02  /* arrows transfer to window     */
#define FR_CSW      0x04  /* window sizes changing regime  */
#define FR_CCR      0x08  /* window coordinates changing reg.*/
#define FR_F10      0x10  /* back to menu                  */
#define FR_IWND     0x20  /* display indicator "window"    */
#define FR_IPCB     0x40  /* display indicator "part"      */
#define FR_NOWE__   0x80  /* don't fill system variables   */
#define FR_CLR     0x100  /* dye regime                    */
#define FR_NOI     0x200  /* don't display indicator       */
#define FR_IND     0x400  /* regime of part creation       */
#define FR_UC      0x800  /* exit on user code             */
#define FR_SPC    0x1000  /* exit on special code          */
#define FR_CURSET 0x2000  /* cursor is set                 */
#define FR_NOHELP 0x4000  /* turn off subsystem HELP       */
#define FR_MOUSE  0x8000U /* exit on mouse                 */
/*--------- bits for rk field of ENV_VRB structure: -------*/
#define RK_SHFT_R   0x01   /* right Shift key pressed      */
#define RK_SHFT_L   0x02   /* left  Shift key pressed      */
#define RK_CNT      0x04          /* Ctrl key pressed      */
#define RK_ALT      0x08          /* Alt  key pressed      */
#define RK_SCR      0x10   /* Scroll Lock key pressed      */
#define RK_NUM      0x20   /*  Num Lock   key pressed      */
#define RK_CAPS     0x40   /*  Caps Lock  key pressed  */
/*------------------- part variables: ---------------------*/
typedef struct
      {char *partname;       /* pointer to part name       */
       PCB *pcb;             /* descriptor of current part */
      } ENV_PART;
/*----------------- variables for help: -------------------*/
typedef struct
      {TINT hlpcor;
       TINT hlpsize;
       TINT indcor, indsize;
       unsigned char hlptype, indtype;
       LDSC *hlpwnd;               /* text of current help */
      } ENV_HELP;
/*----------------- variables for panel: ------------------*/
typedef struct
      {TINT tbcor, tbsize;
       char f1, s1, f2, s2;
       char tablo[80];
       unsigned char tbtype;
       LDSC *tbtext;
      } ENV_TABL;
/*------- substitution of PFUS descriptor -----------------*/
typedef struct
        {int p;                /* call parameter           */
	 WCB *win;             /* window                   */
         PCB *pcb;             /* part                     */
         TINT wcor;            /* environment              */
         TINT tk;
         TINT tmp;
         char *partname;       /* name of current part     */
         unsigned int key;     /* i/o code received        */
         unsigned char liter;  /* symbol ASCII code        */
         unsigned int regk;    /* keyboard registers       */
         TINT oldsize;         /* old window sizes         */
         int brd_sys;
	} US;
/*---------- parameter nf for function a2tv0new: ----------*/
#define F_SETC  0
#define F_CHEC  1
#define F_DISP  2
#define F_CREW  3
#define F_YON   4
#define F_DLTR  10
#define F_CRER  11
#define F_NEWW  12
#define F_ENDL  13
#define F_CLMC  20
#define F_ROWC  30
/*=========================================================*/
#define ER_NOACS -1
#define SZLINE 40            /* substring size             */
#define IDLINE  "TVPOOL"  /* name of memory pool for lines */
#define IDWCB "WCBPOOL" /* name of memory pool for windows */
/*----------------- For range: ----------------------------*/
#define AD_R(W) ((W)->admch)    /* pointer to range values */
#define AD_L(W) ((W)->pattern)   /* pointer to list values */
#define AD_P(W) ((W)->user)      /* pointer to pick values */
#define R_MIN 0x01
#define R_MAX 0x02
#ifdef __cplusplus
extern "C" {
#endif
/*------------ a2tv0new - new windows processing ----------*/
a2tv0new(int nf, ...);   /* function number, parameters... */
/* without parameters: F_SETC - setcur, F_CHEC - checkcur, */
/* F_DISP - displpar, F_CREW - crwindow, F_YON - yon;      */
/* with one parameter: F_DLTR - wdltrow, F_CRER - wcrerow, */
/* F_NEWW - newindow, F_ENDL - newendl;                    */
/* with two parameters: F_CLMC - wclmrow;                  */
/* with three parameters: F_ROWC - wrowcor.                */
/*----- a2sysvar - writing of system variable to window ---*/
void a2sysvar(char *stream, PCB *pcb, WCB *wcb);
/* stream- output flow name or NULL; pcb, wcb-part, window */
/* system names of windows: e__npage, e__date, e__time     */
/*------------- a2s_screen - set size screen --------------*/
void a2s_screen(PCB *cur);
/* cur == NULL - no size screen */
/*---------------- a2clrw - window clearing ---------------*/
LDSC *a2clrw(WCB *wcb);
/*----------- parameter nf for function a2tv0old: ---------*/
#define F_TVDI  0
#define F_WRIT  1
#define F_TABL  2
#define F_SHFT  10
/*------------ a2wcolor - display window in color ---------*/
a2wcolor(WCB *wtm, TINT coor, unsigned char color, int brd_s);
/*------------- a2tv0old - processing of old windows ------*/
a2tv0old(int nf, ...);   /* function number, parameters    */
/* without parameters: F_TVDI - tvdispl, F_WRIT - writeltr,*/
/* F_TABL - dspltbl;                                       */
/*  with one parameter: F_SHFT - shftcur;                  */
/*  with two parameters: F_OUTV - tvouttv;                 */
/*-------------- a2tv0str - save environment  -------------*/
a2tv0kst(void *pfus);
/*-------------- a2tv0rst - restore environment -----------*/
a2tv0krs(void *pfus);
/*- a2tvkalc - translate ALT code to capital letter code --*/
unsigned char a2tvkalc(unsigned int code, int lang);
/* code - full code of key, lang - language: e - English,  */
/* r - Russian, d - primary (by default)                   */
/*----------- a2whatl - language of text ------------------*/
a2whatl(LDSC *ldsc); /* returns: 'e' or 'r' or 'd'         */
/*-- a2tvnalc - number of ALT code of character from list -*/
unsigned char a2tvnalc(unsigned int code, LDSC *ldsc);
/*-------------- a2wndclr - window dye --------------------*/
a2wndclr(int rgm2, PCB *ptm, WCB *wtm);
/*----- a2wcbatr - window attributes correction -----------*/
a2wcbatr(WCB *wcb, TINT *wpos);
/*---- a2objpos - set object display coordinates ----------*/
void a2objpos(int flags, TINT *obpos,
                        TINT *obsize, TINT *pos, TINT *size);
/*- flags: 0 - no frame, else there is a frame; pobj, sobj:*/
/*- what to display; pos, size - display near this object -*/
/*---- a2wcbpos - set coordinates of window to display ----*/
void a2wcbpos(WCB *wcb, TINT *pos, TINT *size);
/*- wcb-window, pos and size - display near this object   -*/
/*---- a2pcbpos - set coordinates of part to display ------*/
void a2pcbpos(PCB *pcb, TINT *pos, TINT *size);
/*- pcb-part, pos and size - display near this object -----*/
/*------ a2ulet - encoding to capital letter code ---------*/
unsigned char a2ulet(unsigned char let);
/*---------- a2setbgl - set capital letters ---------------*/
LDSC *a2setbgl(WCB *wcb);
/* wcb - window containing text with capital letters ------*/
/*--- a2setfbl - set capital letters from 1-st in line ----*/
LDSC *a2setfbl(WCB *wcb);  /* wcb - window containing text */
#ifdef __cplusplus
}
#endif

#define e_h A2e_h
#define e_p A2e_p
#define e_t A2e_t
#define e_v A2e_v

/*------------------ macrodefinitions ---------------------*/
#define PATTERN(wcb) ((wcb)->wsize.row==1 && \
(wcb)->wsize.clm<SZLINE && (wcb)->pattern && \
((wcb)->flagsn & WF_MONEY)==0)

/*---------- macrodefinitions for a2tv0kpc: ---------------*/
/* is vertex terminal ? */
#define  TERM(opt) \
(opt->vt!=TY_S && opt->vt!=TY_M && opt->vt!=TY_A && opt->vt!=TY_R)
#define EDIT (wcb->flagsn & WF_LOOK)==0 /* is editing available */
#define FORMAT wcb->pform[1] > wcb->pform[2]
#define FRMPNT wcb->pform[1]>wcb->pform[2] && !(wcb->pform[0] & 1)
#define MODE e_v.w->flags|=WF_MOD; e_v.r_g &= ~FR_LVP
#define LVP    (e_v.r_g & FR_LVP)==FR_LVP
#define SETELCH(l)  i=l; e_v.ch[i]=(unsigned char)i
#define DISCR e_p.pcb!=NULL && e_p.pcb->part==e_v.w
/*------------------------------------------------------------*/
#define CLRMODE wcb->flags&=~WF_MOD
#define SETLVP e_v.r_g |= FR_LVP
#define SETLVW e_v.r_g &= ~FR_LVP
#define TVCNT1(x) e_t.f1='^'; e_t.s1=x; dspltbl()
#define CLRTVCNT1 e_t.f1=' '; e_t.s1=' '
#define TVCNT2(x) e_t.f2='^'; e_t.s2=x; dspltbl()
#define CLRTVCNT2 e_t.f2=' '; e_t.s2=' '
/*------------------------------------------------------------*/
/* initialization of WCB end, without frame, with frame */
#define SWEND  0,"\0\0\0\0\0\0",{0,0},{0,0},{0,0},\
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,\
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
#define LWEND 0,0,0,"\0\0\0\0\0\0",{0,0},{0,0},{0,0},\
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,\
NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
/* initialization of PCB */
#define IPCB(flp) \
 {flp,0,0,0,0,0,0,\
  NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0}
/*------------- determination of number of alternative ------*/
#define GETNA(ind,start,temp,work) \
 for (ind=0, work=start; work!=temp; ind++, work=work->down);
/*-----------------------------------------------------------*/
/*============== Codes of color for background ==============*/
#define BK_F   0x00                          /* BLACK        */
#define BL_F   0x10                          /* BLUE         */
#define GR_F   0x20                          /* GREEN        */
#define CY_F   0x30                   /* CYAN = BLUE + GREEN */
#define RE_F   0x40                          /* RED          */
#define MA_F   0x50                /* MAGENTA = RED + BLUE   */
#define BR_F   0x60                          /* BROWN        */
#define WH_F   0x70                          /* WHITE        */
/*============== Codes of color for letters =================*/
#define BK_L   0x00
#define BL_L   0x01
#define GR_L   0x02
#define CY_L   0x03                        /* BLUE + GREEN */
#define RE_L   0x04
#define MA_L   0x05                        /* RED + BLUE   */
#define BR_L   0x06
#define WH_L   0x07
#define BR     0x08                     /* high brightness */
/*=========== Regimes for window changing function ========*/
#define R_NO    0                    /* w/p: not set       */
#define R_TXT   1                    /* w/p: text          */
#define R_DS    2        /* w: definition  / p: attributes */
#define R_INTW  3               /* p: entry to 1-st window */
#define R_WATR  3                  /* w: window attributes */
#define R_SZ    4                    /* w/p: sizes         */
#define R_CR    5                    /* w/p: coordinates   */
#define R_CLW   6                  /* w/p: window/part dye */
#define R_CLA   7                       /* w/p: window dye */
#define R_DL    8                    /* w: window deletion */
#define R_CREW  8                    /* p: window creation */
#define R_WOUT  9                   /* w: exit from window */
#define R_POUT  9                     /* p: exit from part */
#define R_PNO  20              /* addition for part window */
/*========= Models for window changing function ===========*/
#define N_CRM   13                     /* number of models */
#define I_MNG    0 /*--- INDEXES OF MODELS --: common menu */
#define I_MNP    1                            /* part menu */
#define I_MNW    2                          /* window menu */
#define I_WATR   3                    /* window attributes */
#define I_PATR   4                      /* part attributes */
#define I_WDSC   5                    /* window descriptor */
#define I_WCHK   6                  /* permissible symbols */
#define I_WCLR   7                         /* window color */
#define I_PCLR   8                           /* part color */
#define I_RNG    9                                /* range */
#define I_FR    10                           /* frame type */
#define I_FWAL  11                         /* letters list */
#define I_HELP  12                           /* promptings */
/*=========================================================*/
#define LEFT_W  0x74     /* ^->                            */
#define UP_LEVEL 0x73    /* ^<-                            */
#define DOWN_LEVEL 0x0D  /* Enter                          */
#define LEVEL_B 0x4A     /* previous on level gray -       */
#define LEVEL_N 0x4E     /* next on level gray +           */
/*=========================================================*/
#define LEFT_SHIFT     0x01    /* Right Shift pressed */
#define RIGHT_SHIFT    0x02    /* Left Shift pressed  */
#define CTRL           0x04    /* Ctrl pressed        */
#define ALT            0x08    /* Alt pressed         */
#define SCROLL_LOCK_ON 0x10    /* Scroll Lock on      */
#define NUM_LOCK_ON    0x20    /* Num Lock on         */
#define CAPS_LOCK_ON   0x40    /* Caps on             */
#define INSERT_ON      0x80    /* Insert on           */
#define LEFT_CTRL      0x0100  /* Left Ctrl pressed   */
#define LEFT_ALT       0x0200  /* Left Alt pressed    */
#define RIGHT_CTRL     0x0400  /* Right Ctrl pressed  */
#define RIGHT_ALT      0x0800  /* Right Alt pressed   */
#define SCROLL_LOCK    0x1000  /* Scroll Lock pressed */
#define NUM_LOCK       0x2000  /* Num Lock pressed    */
#define CAPS_LOCK      0x4000  /* Caps Lock pressed   */
#define SYS_REQ        0x8000U /* Sys Req pressed     */

#define SCR_LOCK 0x10
#define SHIFT    0x03
/*=========================================================*/
/* ASCII codes: */
#define ESC      0x1b
#define DIG0     0x30                      /* digital zero */
#define DIG9     0x39                      /* digital nine */
#define DIGPLS   0x2B                          /* sign  +  */
#define DIGMIN   0x2D                          /* sign  -  */
#define DIGPNT   0x2E                          /* sign  .  */
#define DIGEXB   0x45
#define DIGEXS   0x65
#define LATA     0x41                     /* letter  A */
#define LATZ     0x5A                     /* letter  Z */
#define LATa     0x61                     /* letter  a */
#define LATz     0x7A                     /* letter  z */
#define RUSF     0xB0  /* first russian letter for main set */
#define RUSL     0xEF   /* last russian letter for main set */
#define ALTF     0x80   /* first russian letter for alt set */
#define ALTL     0xAF
#define ALTf     0xE0
#define ALTl     0xEF    /* last russian letter for alt set */

#define MIN_RUS  0x80   /* first letter of Russian alphabet */
#define MAX_RUS  0x9F   /* last letter of Russian alphabet */

#define MIN_ENG  0x41        /* A */
#define MAX_ENG  0x5A        /* Z */

#define BELL     '\07'


#define F1          0x3b
#define F2          0x3c
#define F3          0x3d
#define F4          0x3e
#define F5          0x3f
#define F6          0x40
#define F7          0x41
#define F8          0x42
#define F9          0x43
#define F10         0x44
#define F1_F      0x3B00
#define F2_F      0x3C00
#define F3_F      0x3D00
#define F4_F      0x3E00
#define F5_F      0x3F00
#define F6_F      0x4000
#define F7_F      0x4100
#define F8_F      0x4200
#define F9_F      0x4300
#define F10_F     0x4400

#define SHFTF1      0x54
#define SHFTF2      0x55
#define SHFTF3      0x56
#define SHFTF4      0x57
#define SHFTF5      0x58
#define SHFTF6      0x59
#define SHFTF7      0x5a
#define SHFTF8      0x5b
#define SHFTF9      0x5c
#define SHFTF10     0x5d
#define SHFTF1_F  0x5400
#define SHFTF2_F  0x5500
#define SHFTF3_F  0x5600
#define SHFTF4_F  0x5700
#define SHFTF5_F  0x5800
#define SHFTF6_F  0x5900
#define SHFTF7_F  0x5a00
#define SHFTF8_F  0x5b00
#define SHFTF9_F  0x5c00
#define SHFTF10_F 0x5d00

#define ALTF1       0x68
#define ALTF2       0x69
#define ALTF3       0x6a
#define ALTF4       0x6b
#define ALTF5       0x6c
#define ALTF6       0x6d
#define ALTF7       0x6e
#define ALTF8       0x6f
#define ALTF9       0x70
#define ALTF10      0x71
#define ALT_F10   ALTF10
#define ALT_F1_F  0x6800
#define ALT_F2_F  0x6900
#define ALT_F3_F  0x6A00
#define ALT_F4_F  0x6B00
#define ALT_F5_F  0x6C00
#define ALT_F6_F  0x6D00
#define ALT_F7_F  0x6E00
#define ALT_F8_F  0x6F00
#define ALT_F9_F  0x7000
#define ALT_F10_F 0x7100

#define CNTF1       0x5e
#define CNTF2       0x5f
#define CNTF3       0x60
#define CNTF4       0x61
#define CNTF5       0x62
#define CNTF6       0x63
#define CNTF7       0x64
#define CNTF8       0x65
#define CNTF9       0x66
#define CNTF10      0x67
#define CNT_F1_F  0x5e00
#define CNT_F2_F  0x5f00
#define CNT_F3_F  0x6000
#define CNT_F4_F  0x6100
#define CNT_F5_F  0x6200
#define CNT_F6_F  0x6300
#define CNT_F7_F  0x6400
#define CNT_F8_F  0x6500
#define CNT_F9_F  0x6600
#define CNT_F10_F 0x6700

/*------------ full codes : ----------------------------------*/
#define BKSPC     0x0E08

#define ENTER_F   0x1C0D
#define ENTER       0x0D

#define PROBEL_F  0x3920
#define PROBEL      0x20

#define HOME_F     0x4700
#define HOME         0x47
#define CNT_HOME     0x77
#define CNT_HOME_F 0x7700

#define END_F      0x4f00
#define END          0x4f
#define CNT_END      0x75
#define CNT_END_F  0x7500

#define LEFT_F    0x4B00
#define LEFT        0x4B
#define CNT_LEFT    0x73

#define RIGHT_F   0x4D00
#define RIGHT       0x4D
#define CNT_RIGHT   0x74

#define UP_F      0x4800
#define UP          0x48

#define DOWN_F    0x5000
#define DOWN        0x50

#define PGUP_F     0x4900
#define PGUP         0x49
#define CNT_PGUP     0x84
#define CNT_PGUP_F 0x8400

#define PGDN_F     0x5100
#define PGDN         0x51
#define CNT_PGDN     0x76
#define CNT_PGDN_F 0x7600

#define INS    82
#define DEL    83
#define ESC_F     0x011b
#define TAB_F     0x0f09
#define SHFTTAB_F 0x0f00

#define CNT_Q_F   0x1011
#define CNT_K_F   0x250B
#define CNT_N_F   0x310E
#define CNT_Y_F   0x1519
#define CNT_S_F   0x1F13
#define CNT_L_F   0x260C
#define CNT_F_F   0x2106
#define CNT_A_F   0x1E01
#define CNT_B_F   0x3002
#define CNT_C_F   0x2E03
#define CNT_H_F   0x2308
#define CNT_J_F   0x240A
#define CNT_WB    0x1A1B      /* window start CNTRL [        */
#define CNT_WE    0x1B1D      /* window end   CNTRL ]        */
#define GREYP_F   0x4E2B
#define GREYM_F   0x4A2D

#define	ALT_A_F 0x1C00
#define	ALT_B_F 0x3000
#define	ALT_C_F 0x2E00
#define	ALT_D_F 0x2000
#define	ALT_E_F 0x1200
#define	ALT_F_F 0x2100
#define	ALT_G_F 0x2200
#define	ALT_H_F 0x2300
#define	ALT_I_F 0x1700
#define	ALT_J_F 0x2400
#define	ALT_K_F 0x2500
#define	ALT_L_F 0x2600
#define	ALT_M_F 0x3200
#define	ALT_N_F 0x3100
#define	ALT_O_F 0x1800
#define	ALT_P_F 0x1900
#define	ALT_Q_F 0x1000
#define	ALT_R_F 0x1300
#define	ALT_S_F 0x1F00
#define	ALT_T_F 0x1400
#define	ALT_U_F 0x1600
#define	ALT_V_F 0x2F00
#define	ALT_W_F 0x1100
#define	ALT_X_F 0x2D00
#define	ALT_Y_F 0x1500
#define	ALT_Z_F 0x2C00

#define	INS_F  0x5200
#define	DEL_F  0x5300

#define	W_INS_F			 0x5230
#define	W_DEL_F			 0x532E

#define	G_ENTER_F    0x1C0D
#define	G_ADD_F      0x4E2B
#define	G_SUB_F      0x4A2D
#define	G_MUL_F      0x372A
#define	G_DIV_F      0x352F
#define	W_HOME_F     0x4700
#define	W_UP_F       0x4800
#define	W_PGUP_F     0x4900
#define	W_LEFT_F     0x4B00
#define	W_RIGHT_F    0x4D00
#define	W_END_F      0x4F00
#define	W_DOWN_F     0x5000
#define	W_PGDN_F     0x5100

#define	CNT_ENTER_F  0x1C0A
#define	CNT_LEFT_F   0x7300
#define	CNT_RIGHT_F  0x7400
#define	CNT_END_F    0x7500
#define	CNT_PGDN_F   0x7600
#define	CNT_HOME_F   0x7700
#define	CNT_PGUP_F   0x8400

/*---------------------------------------------------------*/
#define MXTV   MAX_ROW    /* number of lines on screen     */
#define LNTV   161        /* number of characters in line  */
#define MAX_LN 9999       /* maximum length of line        */
#define ER_SHFT -1        /* mistake of shift              */
#define ER_SIZE -2        /* don't fit in window           */
#define ER_PLC  -10       /* symbol in wrong position      */
#define ER_LITER -20      /* inadmissible symbol           */
#define TV_BLK  1
#define TV_PART 2
#define TV_FROM 4
#define TV_ALL  6
/*=========================================================*/

#endif /* H_A2TV0 */
